<?php
/**
 *梭哈游戏
 */ 
class Module_showhand{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 梭哈首页
	 */
	function index(){
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('App_showhand');
		//筹码余额
		loadLib('Currency');
		$stake = Currency::get('app_21' , $uin);
		$smarty->assign('stake' , $stake);
		// 获取自己的战绩
		$gains = App_showhand::getGain($uin , false);
		$newbie = false;
		if(empty($gains)) {
			$gains = App_showhand::getGain($uin , true);
			$newbie = true;
			
		}
		
		$smarty->assign('set_showhand' , $gains['setshowhand']);
		
		// 获取梭哈称号
		$rank = App_showhand::getRank($uin , $gains['gain']);

		// 如果是第一次来，则奖励10000筹码
		if($newbie) {
			loadLib('Currency');
			Currency::update('app_21' , $uin , 10000);
		}
		//获取是否存在该局
		$result = App_showhand::getRandStateList();
		//print_r($result);
		//未完成的梭哈牌局
		$undone = App_showhand::getRandUndoneList($uin);
		//print_r($undone);
		$list = array();
		foreach($result as $k => $row) {
			if($row['uin'] == $uin) continue;	// 不显示自己的
			
			$list[$k] = $row;
			for($i=0;$i<3;$i++){
				$cards[$k][$i]= $list[$k]['state']['owner_cards'][0][$i];
			}
			$cards_str[$k] = App_showhand::num2card($cards[$k]);
		}
		//print_r($cards_str);
		$uin_list = array();
		foreach($list as $row) {
			$uin_list[] = $row['uin'];
			$gains[$row['uin']] = $row['gain'];
		}
		if(!empty($undone)){
			$uin_undone = array();
			foreach($undone as $row) {
				$uin_undone[] = $row['uin'];
				$gain[$row['uin']] = $row['gain'];
			}
		}
	//	print_r($gain);
		loadLib('User');
		//查看可应战的信息
		
		//调用战报
		loadLib('App_battlefie');
		$gains_list=App_battlefie::getBattlefie();

		$smarty->assign('gains_list' , $gains_list);
		// 获取用户等级信息
		$gains_info = App_showhand::getRanks($uin_list , $gains);
		//未完成的局
		if(!empty($undone)){
			$gain_info = App_showhand::getRanks($uin_undone , $gain);
		}
	//	print_r($undone_info);
		if(!empty($uin_list)) {		
			$uin_info = User::getBaseInfo($uin_list);
			foreach($list as $k => $row) {
				$list_uin = $row['uin'];
				$list[$k]['uin_name'] = isset($uin_info[$row['uin']]) ? $uin_info[$row['uin']]['name'] : '';
				$list[$k]['uin_sex'] = isset($uin_info[$row['uin']]) ? $uin_info[$row['uin']]['sex'] : '';
				$list[$k]['rank'] = $gains_info[$list_uin];
				
				$list[$k]['cards'] = isset($cards_str[$k]) ? $cards_str[$k] : '';
				
			}
		}
		//未完成局的信息
		if(!empty($uin_undone)) {		
			$undone_info = User::getBaseInfo($uin_undone);
			foreach($undone as $k => $row) {
				$undone_uin = $row['uin'];
				$undone[$k]['uin_name'] = isset($undone_info[$row['uin']]) ? $undone_info[$row['uin']]['name'] : '';
				$undone[$k]['uin_sex'] = isset($undone_info[$row['uin']]) ? $undone_info[$row['uin']]['sex'] : '';
				$undone[$k]['rank'] = $gain_info[$undone_uin];
			}
		}
	//print_r($gains['setshowhand']);
		$smarty->assign('list' , $list);
		$smarty->assign('undone' , $undone);
		$smarty->assign('rank' , $rank);
		$smarty->assign('newbie' , $newbie);
		
		return $smarty->fetch('app/showhand/index.tpl.php');
	}
	/**
	 * 梭哈排行榜
	 */
	function top(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('rate' , 'gain')) ? trim($_GET['type']) : 'gain';
		
		$smarty = loadSmarty();
		// 获取排行
		loadLib('App_top');
		//获取排行总量
		$count = App_top::getTopCount('app_showhand_gain');
		
	
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$list = App_top::getTopList('app_showhand_gain' , $top_type ,$page,$per_page);
	//	print_r($list);
		$uin_arr = array();
		$gains = array();
		foreach($list as $row) {
			$uin_arr[] = $row['uin'];
			$gains[$row['uin']] = $row['gain'];
		}
		loadLib('App_showhand');
		// 获取用户等级信息
		$gains_info = App_showhand::getRanks($uin_arr , $gains);
	//	print_r($gains);
		loadLib('User');
		$uin_arr_info = User::getBaseInfo($uin_arr);
		foreach($list as $k => $row) {
			$list_uin = $row['uin'];
			$list[$k]['name'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['name'] : '';
			$list[$k]['sex'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['sex'] : '';
			$list[$k]['rank'] = $gains_info[$list_uin];
			$list[$k]['rate'] = sprintf('%.2f' , $row['rate'] / 100);
		}
		
		$smarty->assign('list' , $list);
		$smarty->assign('top_type' , $top_type);
		return $smarty->fetch('app/showhand/top.tpl.php');
	}
	/**
	 * 梭哈开局
	 */
	function open(){
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('Currency');
		$currency = Currency::get('app_21', $uin , 'currency');
		
		// 判断钱是否够，不够就提示去用筹码换，或者退出
		if($currency < 50) {			
			return View::error('一次最少下注50筹码，你现在只有' . $currency . '筹码,去黑市兑换');
		}
		
		Currency::update('app_21' , $uin , -50);
		
		// 随机发五张牌、随机花色
		loadLib('App_showhand');
		
		$cards = array();
		for($i = 0 ; $i < 5 ; $i++) {
    		while(1) {
     		    $card = App_showhand::get_rand();
     		    if(App_showhand::check_same($card , $cards) === false ) {
    	           break;
    	 	    }
  			 }
  			$cards[] = $card;
		}
		$drop = App_showhand::numcard($cards);
		
		$fairly = App_showhand::getfairly($drop);
		$id = 0;
		$cards=array($cards,$fairly,0,0);
		/****************几率获得道具*****30%************/
		loadLib('App_kingdomslady');
		$rand = App_kingdomslady::get_chance(30);
		if ($rand == 1){
			loadLib('App_prop');
			$prop = App_prop::getOneselfProp('app_showhand_oneself_prop', $uin, 2);
			/**********************记录为空时写入************************/
			if (empty($prop)){
				App_prop::getAddOneselfProp('app_showhand_oneself_prop', $uin, 2, 1);
			}else{
				//$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
				$count = $prop['count'] + 1;
				App_prop::getAlterOneselfProp('app_showhand_oneself_prop', 2, $uin, $count, $prop['time']);
			}
		}
		$cards['rand'] = $rand;
		$code = App_showhand::getCordState($uin , $id , 0 , 1 , $cards , 50);
		loadLib('Operate');
		Operate::getAdd($uin , 0 , 'app_showhand');
		//防止刷新
		header('Location: ' . SITEURL . '/app.php?m=showhand&a=addcard&uin=' . sessionInfo('uin') . '&id=' . $id . '&sid=' . sessionInfo('skey') );
	}
	/**
	 * 开局成功
	 * */
	function addcard(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$smarty = loadSmarty();
		loadLib('App_showhand');

		$state = App_showhand::getState($owner_uin , $id);
//print_r($state);
		if(empty($state)) {
			return View::error('该局已结束');
		}
	
		$great=array();
		if(!empty($state['state']['owner_cards'][1][1][1])){
			$great[0] = App_showhand::num2card($state['state']['owner_cards'][1][1][0]);
			$great[1] = App_showhand::num2card($state['state']['owner_cards'][1][1][1]);	
		}else{
			$great[] = App_showhand::num2card($state['state']['owner_cards'][1][1]);
		}

		//从大到小排列
		$cards = App_showhand::numcard($state['state']['owner_cards'][0]);
		
		$cards_str = App_showhand::num2card($cards);
		
		$random = $state['state']['owner_cards'][2];
		$gains = App_showhand::getGain($owner_uin , true);
		$smarty->assign('set_showhand',$gains['setshowhand']);
		/***********************道具列表**********************/
		loadLib('App_prop');
		$oneselfprop = App_prop::getAllOneselfProp('app_showhand_oneself_prop', 'app_showhand_prop', $owner_uin);
		
		foreach ($oneselfprop as $k => $row){
			if ($row['type'] == 'random'){
				$list[] = $row;
			}
		}
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
		$rand = $state['state']['owner_cards'];
		$smarty->assign('rand',$rand);
		$smarty->assign('fairly',$state['state']['owner_cards'][1]);
		$smarty->assign('uin',$owner_uin);
		$smarty->assign('id',$id);
		$smarty->assign('great',$great);
		$smarty->assign('random',$random);
		$smarty->assign('cards_str',$cards_str);
		return $smarty->fetch('app/showhand/open.tpl.php');
	}
	/**
	 * 梭哈换牌
	 */
	function addcards(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		// 随机发五张牌、随机花色
		loadLib('App_showhand');

		$smarty = loadSmarty();
		
		
		$cards = array();
		for($i = 0 ; $i < 5 ; $i++) {
    		while(1) {
     		    $card = App_showhand::get_rand();
     		    if(App_showhand::check_same($card , $cards) === false ) {
    	           break;
    	 	    }
  			 }
  			$cards[] = $card;
		}
		$drop = App_showhand::numcard($cards);
		//print_r($drop);
		$fairly = App_showhand::getfairly($drop);
		//print_r($fairly);
		$cards = array($cards,$fairly,1,0);
		
		//写成数组
		$code = App_showhand::getAddcards($owner_uin , $id , $cards);
		
		if($code == 0){
			//防止刷新
			header('Location: ' . SITEURL . '/app.php?m=showhand&a=addcard&uin=' . sessionInfo('uin') . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		}
	}
	/**
	 * 加注页面
	 */
	function end(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$smarty = loadSmarty();
		loadLib('App_showhand');
		
		$state = App_showhand::getState($owner_uin , $id);
		
		if(empty($state)) {
			return View::error('该局已结束');
		}
		loadLib('Currency');
		$balance = Currency::get('app_21' , $owner_uin);
		$smarty->assign('balance',$balance);
		if(!empty($state['state']['owner_cards'][1][1][1])){
			$great[0] = App_showhand::num2card($state['state']['owner_cards'][1][1][0]);
			$great[1] = App_showhand::num2card($state['state']['owner_cards'][1][1][1]);	
		}else{
			$great[] = App_showhand::num2card($state['state']['owner_cards'][1][1]);
		}

		//从大到小排列
		$cards = App_showhand::numcard($state['state']['owner_cards'][0]);
		
		$cards_str = App_showhand::num2card($cards);
		//读取个人表，取得设置状态
		$gains = App_showhand::getGain($owner_uin , true);
		/***********************道具列表**********************/
		loadLib('App_prop');
		$oneselfprop = App_prop::getAllOneselfProp('app_showhand_oneself_prop', 'app_showhand_prop', $owner_uin);
		foreach ($oneselfprop as $k => $row){
			if ($row['type'] == 'dormant'){
				$list[] = $row;
			}
		}
		
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
		$smarty->assign('set_showhand',$gains['setshowhand']);
		$smarty->assign('fairly',$state['state']['owner_cards'][1]);
		$smarty->assign('uin',$owner_uin);
		$smarty->assign('id',$id);
		$smarty->assign('is_list',$state);
		//print_r($state);
		$smarty->assign('stake',$state['stake']);
		$smarty->assign('great',$great);
		$smarty->assign('cards_str',$cards_str);
		
		return $smarty->fetch('app/showhand/addstake.tpl.php');
	}
	/**
	 * 加注操作
	 */
	function addstake(){
		$owner_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$add_stake=isset($_POST['add_stake']) ? intval($_POST['add_stake']) : null;
		
		loadLib('App_showhand');
		
		$state = App_showhand::getState($owner_uin , $id);
		
		if(empty($state)) {
			return View::error('该局已结束');
		}
		// 判断用户是否够钱
		if($add_stake <= 0) {
			header('Location: ' . SITEURL . '/app.php?m=showhand&a=openshowhand&id='.$id.'&sid=' . sessionInfo('skey') );
		}
		
		loadLib('Currency');
		
		$currency = Currency::get('app_21', $owner_uin , 'currency');
			
		// 判断钱是否够，不够就提示去用金币换，或者退出
		if($currency < $add_stake) {			
			return View::error('你的筹码不够 ' . $add_stake . '，不能加注,去黑市兑换');
		}
		$code = Currency::update('app_21' , $owner_uin , -$add_stake);
		
		if($code === 0){

			$stake = $state['stake']+$add_stake;
			App_showhand::addstake($owner_uin , $id , 1 , $stake);
			
			return View::success('加注 ' . $add_stake . ' 筹码成功');
			
		}else{
			return View::error('操作失败' , $code);
		}
		return $smarty->fetch('app/showhand/addstake.tpl.php');
	}
	/**
	 * 查看梭哈牌局
	 */
	function openshowhand(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		
		$uin = sessionInfo('uin');
		loadLib('App_showhand');
		$count = App_showhand::getOneCount($uin);
		
		$smarty = loadSmarty();
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		//计算总页数
		$list = App_showhand::getOneList($uin , $page , $per_page);
		
		//查询游戏设置操作
		$set_showhand = App_showhand::getGain($uin , true);
		if(!empty($list)){
			foreach($list as $k=>$v){
				$cards[] = $list[$k]['state']['owner_cards'][0];
			}

			foreach($cards as $n=>$m){
				$cards_str[] = App_showhand::num2card($cards[$n]);
			}
			$smarty->assign('cards_str',$cards_str);
		}
		$smarty->assign('list' , $list);
		$smarty->assign('set_showhand' , $set_showhand['setshowhand']);
		$smarty->assign('per_page' , $per_page);
		$smarty->assign('count' , $count['count']);
		return $smarty->fetch('app/showhand/myopen.tpl.php');
	}
	/**
	 * 应战梭哈随机出牌
	 */
	function challenge(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$uin = sessionInfo('uin');
		loadLib('App_showhand');
		
		$list = App_showhand::getState($owner_uin , $id);
		if(empty($list)) {
			return View::error('该局已结束');
		}
		
		if($list['guest_uin'] > 0 || $list['turn'] != 1) {
			return View::error('你下手太慢拉，别人已经应战了');
		}
		//print_r($uin);
		$judge = App_showhand::getOneList($uin , 1 , 1);
		//print_r($judge);
		if(empty($judge)) {
			return View::error('你要自己开一局后才能应战别人,现在去开一局');
		}
		loadLib('Currency');
		$currency = Currency::get('app_21', $uin , 'currency');
		//print_r($currency);
		// 判断钱是否够，不够就提示去用金币换，或者退出
		if($currency < $list['stake']) {			
			return View::error('你的筹码不够 ' . $list['stake'] . ',不能应战,去黑市兑换');
		}
		
		$code = Currency::update('app_21' , $uin , -$list['stake']);
		
		$stake = $list['stake']*2;
		App_showhand::addstake($owner_uin , $id , 2 , $stake);
		loadLib('Operate');
		Operate::getAdd($uin , $owner_uin , 'app_showhand_challenge');
		$cards_str[] = $list['state']['owner_cards'][0];
		
		App_showhand::getUpState($id, $owner_uin, $uin);
		
		$cards = array();
		for($i = 0 ; $i < 5 ; $i++) {
    		while(1) {
     		    $card = App_showhand::get_rand();
     		    if(App_showhand::check_same($card , $cards) === false && App_showhand::check_same($card , $cards_str) === false) {
    	           break;
    	 	    }
  			 }
  			$cards[] = $card;
		}
		$drop=App_showhand::numcard($cards);
		$fairly=App_showhand::getfairly($drop);
		$cards=array($cards,$fairly,0);
		
		$code=App_showhand::getUpShowhand($id , $owner_uin , $cards , $list);
		
		$smarty = loadSmarty();
		if($code == 0){
			//防止刷新
			header('Location: ' . SITEURL . '/app.php?m=showhand&a=endguest&uin=' . sessionInfo('uin') . '&owner_uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		}
	}
	/**
	 * 应战再次换牌
	 */
	function guestrand(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		loadLib('App_showhand');
		
		$list = App_showhand::getState($owner_uin , $id);
		if(empty($list)) {
			return View::error('该局已结束');
		}
		//取得A玩家的牌
		$cards_str[] = $list['state']['owner_cards'][0];
		
		$cards = array();
		for($i = 0 ; $i < 5 ; $i++) {
    		while(1) {
     		    $card = App_showhand::get_rand();
     		    if(App_showhand::check_same($card , $cards_str[0]) === false && App_showhand::check_same($card , $cards) === false ) {
    	           break;
    	 	    }
  			 }
  			
  			$cards[] = $card;
		}
		
		$drop = App_showhand::numcard($cards);
		$fairly = App_showhand::getfairly($drop);
		$cards = array($cards,$fairly,1);
		
		$code = App_showhand::getUpShowhand($id , $owner_uin , $cards , $list);
		
		$smarty = loadSmarty();
		if($code == 0){
			//防止刷新
			header('Location: ' . SITEURL . '/app.php?m=showhand&a=endguest&uin=' . sessionInfo('uin') . '&owner_uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		}
	}
	/**
	 *梭哈应战牌局页面
	 */
	function endguest(){
		$owner_uin = isset($_GET['owner_uin']) ? intval($_GET['owner_uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		$smarty = loadSmarty();
		loadLib('App_showhand');
		
		$state = App_showhand::getState($owner_uin , $id);
		
		if(empty($state)) {
			return View::error('该局已结束');
		}
		//判断是否使用随机一次
		$random = $state['state']['guest_cards'][2];
		loadLib('User');
		$uin_arr_info = User::getBaseInfo($state['uin']);
		for($i = 0;$i < 3;$i++){
			$cards['owner_cards'][] = $state['state']['owner_cards'][0][$i];
		}

		$cards[1] = App_showhand::numcard($state['state']['guest_cards'][0]);
		//print_r($cards['owner_cards']);
		$cards_str['owner_cards'] = App_showhand::num2card($cards['owner_cards']);
		$cards_str['guest_cards'] = App_showhand::num2card($cards[1]);
		$cards_str['owner_cards'][3]=array('a'=>"1",'b'=>"BP");
		$cards_str['owner_cards'][4]=array('a'=>"1",'b'=>"BP");
		//print_r($cards_str);
		$uin = sessionInfo('uin');
		$gains = App_showhand::getGain($uin , true);
		$smarty->assign('set_showhand',$gains['setshowhand']);
		
		$smarty->assign('uin_arr_info',$uin_arr_info);
		
		$smarty->assign('uin',$owner_uin);
		$smarty->assign('id',$id);
		$smarty->assign('stake',$state['stake']);
		/***********************道具列表**********************/
		loadLib('App_prop');
		$oneselfprop = App_prop::getAllOneselfProp('app_showhand_oneself_prop','app_showhand_prop',$uin);
		foreach ($oneselfprop as $k => $row){
			if ($row['type'] != 'dormant'){
				$list[] = $row;
			}
		}
		
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
		//print_r($state);
		$smarty->assign('is_list',$state);
		$smarty->assign('cards_str',$cards_str);
		$smarty->assign('random',$random);
		return $smarty->fetch('app/showhand/fight.tpl.php');
	}
	/**
	 * 结束对战
	 */
	function endfight(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_showhand');
		
		$state = App_showhand::getState($owner_uin , $id);

		if(empty($state)) {
			return View::error('该局已结束');
		}
		$gains = App_showhand::getGain($uin , true);
		$smarty->assign('set_showhand',$gains['setshowhand']);
		//梭哈牌局的钱
		$stake=$state['stake']/2;
		$stak=intval($stake*0.9)+$stake;
		
		loadLib('User');
		$uin_arr_info = User::getBaseInfo($state['uin']);
		
		$cards[] = App_showhand::numcard($state['state']['owner_cards'][0]);
		$cards[] = App_showhand::numcard($state['state']['guest_cards'][0]);
	
		$cards_str['owner_cards'] = App_showhand::num2card($cards[0]);
		$cards_str['guest_cards'] = App_showhand::num2card($cards[1]);
//	print_r($cards_str['owner_cards']);
		
		$fairly[] = $state['state']['owner_cards'][1];
		$fairly[] = $state['state']['guest_cards'][1];
		$smarty->assign('squeezer',$fairly);
		//A玩家信息
		$owner_player = App_showhand::getGain($owner_uin , true);
		//B玩家信息
		$guest_player = App_showhand::getGain($uin , true);
		//print_r($fairly);
		//判断牌的大小
		$fairly=App_showhand::getEndFairly($fairly);
		
		
		loadLib('Currency');
		if($fairly == 2){
			//A玩家信息
			$owner_gain = $owner_player['gain'] - 25;
			$owner_total = $owner_player['total'] + 1;
			$owner_win = $owner_player['win'];
			$owner_rate = $owner_win / $owner_total * 10000;
			$owner_rate = intval($owner_rate);
			//B玩家信息
			$guest_gain = $guest_player['gain'] + 25;
			$guest_total = $guest_player['total'] + 1;
			$guest_win = $guest_player['win'] + 1;
			$guest_rate = $guest_win / $guest_total * 10000;
			$guest_rate = intval($guest_rate);
			$wins=2;
			$money=$stake;
			Currency::update('app_21' , $uin , +$stak);
		}else{
			//A玩家信息
			$owner_gain = $owner_player['gain'] + 25;
			$owner_total = $owner_player['total'] + 1;
			$owner_win = $owner_player['win'] + 1;
			$owner_rate = $owner_win / $owner_total * 10000;
			$owner_rate = intval($owner_rate);
			//B玩家信息
			$guest_gain = $guest_player['gain'] - 25;
			$guest_total = $guest_player['total'] + 1;
			$guest_win = $guest_player['win'];
			$guest_rate = $guest_win / $guest_total* 10000;
			$guest_rate = intval($guest_rate);
			$wins=1;
			$money=$stak;
			Currency::update('app_21' , $owner_uin , +$stak);
		}
		//print_r($guest_gain . $owner_gain);
		//print_r($owner_win);
		App_showhand::getAlterGain($owner_uin,$owner_gain,$owner_total,$owner_win,$owner_rate);
			
		App_showhand::getAlterGain($uin,$guest_gain,$guest_total,$guest_win,$guest_rate);
		loadLib('SysMsg');
			SysMsg::sendSystem($owner_uin , array(
				'owner' =>$cards_str['owner_cards'],
				'guest'=>$cards_str['guest_cards'],
				'send_uin' => $uin,
				'win' => $wins,
				'earn' => $money,
			) , 1006);
		App_showhand::delState($owner_uin, $id);
		//print_r($fairly);
		$smarty->assign('uin_arr_info',$uin_arr_info);
		
		$smarty->assign('fairly',$fairly);
		$smarty->assign('stake',$stake);
		//print_r($state['stake']);
		$smarty->assign('cards_str',$cards_str);
		
		return $smarty->fetch('app/showhand/endguest.tpl.php');
	
	}
	/**
	 * 梭哈道具
	 */
	function showhandproperty(){
		$smarty = loadSmarty();
		loadLib('App_showhand');
		$uin = sessionInfo('uin');
		// 获取自己的战绩
		$gains = App_showhand::getGain($uin , true);
		//获取梭哈道具
		loadLib('App_prop');
		$prop = App_prop::getAllProp('app_showhand_prop');
		
		$smarty->assign('list' , $prop);
	
		// 获取盈利
		$rank = App_showhand::getRank($uin , $gains['gain']);
		$oneselfprop = App_prop::getAllOneselfProp('app_showhand_oneself_prop', 'app_showhand_prop', $uin);
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		//print_r($oneselfprop);
		$smarty->assign('oneselfprop' , $oneselfprop);
		$smarty->assign('money',$money);
		$smarty->assign('rank' , $rank['name']);
		return $smarty->fetch('app/showhand/property.tpl.php');
	}
	/**
	 * 购买道具
	 */
	function addProp(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$photoscope = isset($_POST['photoscope']) ? intval($_POST['photoscope']) : null;
		if (empty($id) || empty($photoscope)){
			return View::error('参数错误!!!');
		}
		
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_showhand_prop', $id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		$uin=sessionInfo('uin');
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		$gold = $photoscope * $list['price'];
		
		if ($gold > $money['gold']){
			return View::error('你的金币不足'.$gold.',请及时充值!!!');
		}
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('app_showhand_oneself_prop', $uin, $id);
		/**********************记录为空时写入************************/
		if (empty($prop)){
			App_prop::getAddOneselfProp('app_showhand_oneself_prop', $uin, $id, $photoscope);
		}else{
			//$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			$count = $prop['count'] + $photoscope;
			App_prop::getAlterOneselfProp('app_showhand_oneself_prop', $id, $uin, $count, $prop['time']);
		}
		/***********************扣除金币********************/
		$code = Money::update($uin, -$gold);
		if($code === 0){
			$money = $money['gold'] - $gold;
			//消费记录
			loadLib('Operate');
			Operate::getAddConsume($uin, 'app_showhand_prop', $id, $gold);
			return View::success('你购买了' . $photoscope .'个'.$list['name'].',你的余额为' . $money . '金币');
		}else{
			return View::error('购买失败');
		}
	}
	/**
	 * 使用道具
	 */
	function scene(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$prop_id = isset($_GET['prop']) ? intval($_GET['prop']) : null;
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_showhand');
		
		$state = App_showhand::getState($owner_uin , $id);

		if(empty($state)) {
			return View::error('该局已结束');
		}
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_showhand_prop', $prop_id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		if ($owner_uin == $uin){
			if (!empty($state['state']['prop'])){
				if ($state['state']['prop'] == 'dormant'){
					return View::error('你已使用该道具,不能重复使用!!!');
				}
			}
		}else{
			if ($list['type'] != 'random'){
				if (!empty($state['state']['prop'])){
					if ($state['state']['prop'] == 'perspective'){
						return View::error('你已使用该道具,不能重复使用!!!');
					}
				}
			}
		}
		
		
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('app_showhand_oneself_prop', $uin, $prop_id);
		if (empty($prop)){
			return View::error('你没有此道具!!!' , 0 ,array(
				array('name' => '前往商店' , 'url' => '/app.php?m=showhand&amp;a=showhandproperty&amp;sid=' . sessionInfo('skey')),
				));
		}
		if (!empty($state['state']['prop'])){
			if ($state['state']['prop'] == 'dormant'){
				$state['state']['prop'] = '';
				$state['state']['prop_state'] = 8;
			}
		}else{
			$state['state']['prop'] = $list['type'];
			
		}
		
		$code=App_showhand::getAlterStateShowhand($id , $owner_uin , $state['state']);
		/*****************透视*****************/
		if ($list['type'] == 'perspective') {
			
			//防止刷新
			header('Location: ' . SITEURL . '/app.php?m=showhand&a=endguest&uin=' . sessionInfo('uin') . '&owner_uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );	
		}
		/******************随机****************/
		if ($list['type'] == 'random') {
			$prop_state = isset($_GET['prop_state']) ? intval($_GET['prop_state']) : null;
			if (empty($prop_state)){
				//防止刷新	
				header('Location: ' . SITEURL . '/app.php?m=showhand&a=guestrand&uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
			}elseif ($prop_state == 8){
				header('Location: ' . SITEURL . '/app.php?m=showhand&a=addcards&uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
			}else{
				return View::error('非法操作!!!');
			}
		}
		/******************隐身****************/
		if ($list['type'] == 'dormant') {
			
			//防止刷新	
			header('Location: ' . SITEURL . '/app.php?m=showhand&a=end&uin=' . sessionInfo('uin') . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		}
		if ($code == 0){
			$count = $prop['count'] -1;
			if ($count <= 0){
				App_prop::delProp('app_showhand_oneself_prop', $uin, $prop_id);
			}else{
				App_prop::getAlterOneselfProp('app_showhand_oneself_prop', $prop_id, $uin, $count, $prop['time']);
			}
		}else{
				return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 删除梭哈牌局,扣除当前牌局的10%筹码
	 */
	function delete_showhand(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$stake = isset($_POST['stake']) ? intval($_POST['stake']) : 0;
		if(empty($id)){
			return View::error('参数错误!');
		}
		$uin = SessionInfo('uin');
		$stake = $stake*0.9;
		loadLib('App_showhand');
		$code = App_showhand::delState($uin, $id);
		if($code === 0){
			loadLib('Currency');
			Currency::update('app_21' , $uin , +$stake);			
			header('Location: ' . SITEURL . '/app.php?m=showhand&sid=' . sessionInfo('skey') );
		}else{
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	/**
	 * 梭哈玩法
	 */
	function showhandstate(){
		$smarty = loadSmarty();

		return $smarty->fetch('app/showhand/state.tpl.php');
	}
	/**
	 * 游戏设置页面
	 */
	function set_showhand_initial(){
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		loadLib('App_showhand');
		$gains = App_showhand::getGain($uin , true);
		//print_r($gains);
		$smarty->assign('set_showhand',$gains['setshowhand']);
		return $smarty->fetch('app/showhand/set_showhand.tpl.php');
	}
	/**
	 * 开始设置showhand
	 */
	function set_showhand(){
		$set_num = isset($_POST['set_num']) ? intval($_POST['set_num']) : 0;
		
		$uin = SessionInfo('uin');
		loadLib('App_showhand');
		$code = App_showhand::getAlterShowhand($uin , $set_num);
		if($code == 0){
			header('Location: ' . SITEURL . '/app.php?m=showhand&sid=' . sessionInfo('skey') );
		}else{
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
}
# end of script